package com.lb.shopguide.ui.fragment.guide.order;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.AdapterGoodsInChangePrice;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.order.GoodsInOrderBean;
import com.lb.shopguide.event.guide.ChangePriceEvent;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kang on 2017/6/19.
 */

public class FragmentChangePrice extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private List<GoodsInOrderBean> mGoodsInOrderList;
    private AdapterGoodsInChangePrice mAdapterGoods;

    public static FragmentChangePrice newInstance(List<GoodsInOrderBean> list) {
        FragmentChangePrice fragmentChangePrice = new FragmentChangePrice();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(AppConstant.GOODS_DETAIL_LIST, (ArrayList<? extends Parcelable>) list);
        fragmentChangePrice.setArguments(bundle);
        return fragmentChangePrice;
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_change_price;
    }

    @Override
    public void initData() {
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.GOODS_DETAIL_LIST)) {
            mGoodsInOrderList = bundle.getParcelableArrayList(AppConstant.GOODS_DETAIL_LIST);
        }
    }

    @Override
    public void initView() {
        mRecyclerView = view.findViewById(R.id.recyclerView);
        ntb = view.findViewById(R.id.ntb);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapterGoods = new AdapterGoodsInChangePrice(R.layout.item_goods_in_order, mGoodsInOrderList);
        mRecyclerView.setAdapter(mAdapterGoods);
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).paintProvider(mAdapterGoods).build());

        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setTitleText("改价");
        ntb.setRightText("保存");

        mAdapterGoods.setEnableLoadMore(false);
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.smoothScrollToPosition(mGoodsInOrderList.size() - 1);
            }
        }, 200);

        mAdapterGoods.setOnItemEditTextChangedListener(new AdapterGoodsInChangePrice.OnItemEditTextChangedListener() {
            @Override
            public void onEditTextAfterTextChanged(Editable editable, int position) {
                GoodsInOrderBean item = mGoodsInOrderList.get(position);
                String discount = editable.toString();
                if (discount.equals(".")) {
                    return;
                }
                if (TextUtils.isEmpty(discount)) {
                    item.setDiscountPrice(item.getPrice());
                    return;
                }
                if (Float.parseFloat(discount) > Float.parseFloat(item.getPrice())) {
                    item.setNotValid(true);
                } else {
                    item.setDiscountPrice(discount);
                    item.setNotValid(false);
                }
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentChangePrice.class.getName();
    }

    @OnClick(R.id.image_left)
    void popClick(){
        pop();
    }

    @OnClick(R.id.tv_right)
    void rightClick(){
        if (checkAndSave()) {
            ToastUtils.showShort("请设置合理的优惠价格");
        } else {
            EventBus.getDefault().post(new ChangePriceEvent(mGoodsInOrderList));
            pop();
            KeyboardUtils.hideSoftInput(_mActivity);
        }
    }

    private boolean checkAndSave() {
        boolean flag = false;
        for (int i = 0; i < mGoodsInOrderList.size(); i++) {
            GoodsInOrderBean bean = mGoodsInOrderList.get(i);
            if (bean.isNotValid()) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}
